//
//  QLCicleOfFriendsImaView.m
//  神约
//
//  Created by qlwl on 16/12/14.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLCicleOfFriendsImaView.h"
#import "UIView+FrameKIt.h"
#import "QLCircleOfFriendsLisModel.h"
#import "MJPhotoBrowser+MyPhoto.h"

#define transverseSpace 4
#define verticalSpace transverseSpace


@implementation QLCicleOfFriendsImaView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)setImaItemArray:(NSArray *)imaItemArray
{
    _imaItemArray = imaItemArray;
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (_imaItemArray.count == 0) {
        self.f_height = 0;
        self.hidden = YES;
        return;
    }
    
    
    
    CGFloat itemW = [self widthFromeItemCount:_imaItemArray.count];
    CGFloat itemH = 0;

    if (self.resoucType == ResouceOfVideo) {
        itemH = itemW * 9/16;
    }else{
        if (_imaItemArray.count == 1) {
            //        UIImage *image = [UIImage imageNamed:_imaItemArray.firstObject];
            //        if (image.size.width) {
            //            itemH = image.size.height / image.size.width * itemW;
            //        }
            itemH = itemW;
        } else {
            itemH = itemW;
        }
    }
    
    long perRowItemCount = [self perRowItemCountForPicPathArray:_imaItemArray.count];
    CGFloat margin = transverseSpace;
    
    [_imaItemArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        long columnIndex = idx % perRowItemCount;
        long rowIndex = idx / perRowItemCount;
        UIImageView *imageView = [UIImageView new];
        imageView.backgroundColor = [UIColor lightGrayColor];
        imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
        [self addSubview:imageView];
        
        id objc = _imaItemArray[idx];
        if (self.resoucType == ResouceOfVideo) {
            if ([objc isKindOfClass:[QLCircleOfVideosModel class]]) {
                QLCircleOfVideosModel * model = (QLCircleOfVideosModel *)objc;
                NSLog(@"%@",[NSString stringWithFormat:@"%@%@",HTTP_BaseURL,model.videopic]);
                [imageView QL_setImageWithURL:[NSString stringWithFormat:@"%@%@",HTTP_BaseURL,model.videopic] placeholderImage:SkillOrDemandDefaultIma];
            }
            UIButton * playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            playBtn.tag = idx;
            playBtn.frame = imageView.bounds;
            [playBtn setImage:[UIImage imageNamed:@"circlePlayVidio视频"] forState:UIControlStateNormal];
            [self addSubview:playBtn];
            [playBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            [imageView QL_setImageWithURL:[NSString stringWithFormat:@"%@%@",HTTP_BaseURL,objc] placeholderImage:SkillOrDemandDefaultIma];
            imageView.userInteractionEnabled = YES;
            imageView.tag = idx;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
            [imageView addGestureRecognizer:tap];
        }
    }];
    
    float w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
    int itemCount = (int)_imaItemArray.count;
    int columnCount = itemCount%perRowItemCount==0?itemCount/perRowItemCount:itemCount/perRowItemCount+1;
    float h = columnCount * itemH + (columnCount - 1) * margin;
    self.f_width = w;
    self.f_height = h;
}

- (void)tapImageView:(UITapGestureRecognizer *)tap
{
    NSMutableArray * imaArrays = [[NSMutableArray alloc] init];
    for (NSString * pics in self.imaItemArray) {
        [imaArrays addObject:[NSString stringWithFormat:@"%@%@",HTTP_BaseURL,pics]];
    }
    [MJPhotoBrowser showMyMJPhotoBrowserWithImaURLArray:imaArrays andImaView:(UIImageView *)tap.view andIndex:(int)tap.view.tag];
    if (self.showPhoto) {
        self.showPhoto(tap.view.tag);
    }
}
- (void)playBtnClick:(UIButton *)btn
{
    QLCircleOfVideosModel * model = self.imaItemArray[btn.tag];
    //播放视频
    if (self.palyVideo) {
        self.palyVideo(model.url);
    }
}

- (CGFloat)widthFromeItemCount:(NSInteger)count
{
    if (self.resoucType == ResouceOfVideo) {
        return 180 * ScreenWidthScale;
    }
    if (count == 1) {
        return 110 * ScreenWidthScale;
    } else {
        return 70 * ScreenWidthScale;
    }
}

- (NSInteger)perRowItemCountForPicPathArray:(NSInteger)count
{
    if (count <= 3) {
        return count;
    } else if (count <= 4) {
        return 2;
    } else {
        return 3;
    }
}

@end
